package com.igeek.health.job;

import com.igeek.health.constant.RedisConstant;
import com.igeek.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @Description TODO
 * @Author chenmin
 * @Version 1.0
 * @Date 2023/2/21 15:45
 */
@Component
public class CleanImgJob {

    @Autowired
    private JedisPool jedisPool;

    //编写一个定时任务，根据Redis中的存储信息，清除七牛云上的垃圾图片
    public boolean clearImg(){
        System.out.println("----clearImg-----");
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC, RedisConstant.SETMEAL_DB_PIC);
        if(set!=null && set.size()>0){
            for (String pic : set) {
                //清除七牛云上的垃圾图片
                boolean flag = QiniuUtils.deleteFromQiniu(pic);
                if(flag){
                    //从Redis中删除
                    Long l = jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC, pic);
                    System.out.println("自定义任务执行，清理垃圾图片："+pic);
                    return l>0?true:false;
                }
            }
        }
        return false;
    }

}
